- /* sifissub.cpp by K.Tsuru */
- // function ID = 423 BRADIX
- /************************************************
- SInteger class
- It provides a subtraction "SInteger-unsigned short".
- It is used in operator--();
- result = m - s ( s < BRADIX)
- *************************************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
-
- void IsSub(const SInteger& m, fType s, SInteger& result){
- if(s >= BRADIX) m.SetError(m.OUT_OF_RANGE, "IsSub", 423);
- result = m; // copy
- if(!s) return;
- // result == m
- if(m.Sign(423)<0){ // m < 0
- // m - s = -(|m|+s)
- result.SetSign(1); IsAdd(result, s, result); result.SetSign(-1);
- return;
- }
- // ------ In the case of m > 0 ------
- fType u = result.figure(0) - s; //u>BRADIX for result.figure(0)<s
-
- if( (m.aHead == 0) && (u == 0) ){ // m == s
- result.SetZero(); return;
- }
-
- register uint i = 0;
- if(u < BRADIX){
- result.figure[0] = u;
- while( !result.figure(i) ) i++;
- result.aTail = i;
- return;
- }
- // u >= BRADIX ( m(0) < s )
- if(!m.aHead){ // m has one figure and m < s.
- result.figure[0] = s - m[0];
- result.SetSign(-1);
- return;
- }
- // Normalize u >= BRADIX and m.aHead >= 1
- fType* rv = result.figure.Elements();
- rv[0] = u & BRADIX1;
- u = u >> BRADIX_BITS; //borrow u =1
-
- i = 0;
- while(u){
- i++;
- u = rv[i] - u;
- rv[i] = u & BRADIX1;
- u = u >> BRADIX_BITS;
- }
- i = m.aHead;
- #ifndef NDEBUG
- result.figure(i);
- #endif
- while(!rv[i]) i--;
- result.aHead = i;
- i = 0;
- while(!rv[i]) i++;
- result.aTail = i;
- if(2u*(result.aHead+1) <= result.figure.size()) result.DoCutDown();
- }
sifissub.cpp : last modifiled at 2017/03/13 14:31:59(1,590 bytes)
created at 2016/04/25 14:53:17
The creation time of this html file is 2017/10/25 11:09:45 (Wed Oct 25 11:09:45 2017).